package bs.app.Component;

import android.os.Build;
import bs.app.Main.CCanvas;
import bs.app.Res.CRess;
import bs.app.Util.BitMapFont;
import bs.app.android.Graphics;
public class TextField 
{
    public static String[] print = { " 0", ".,?!_1\"/@$-():*+<=>;%&~#%^&*{}[];\'/1", "abc2áàảãạâấầẩẫậăắằẳẵặ2", "def3đéèẻẽẹêếềểễệ3", "ghi4íìỉĩị4", "jkl5", "mno6óòỏõọôốồổỗộơớờởỡợ6", "pqrs7", "tuv8úùủũụưứừửữự8", "wxyz9ýỳỷỹỵ9", "*", "#" };
    public String TextString="";
    public static final int CharHeight=13;
    public int lastKey = -1983;
    public int delaytimeCaret=0;
    public static int maxTextLength=25;
    public int xCaret,yCaret,indexOfActiveChar;
    public int x,y,width,height;
    public int ActiveKey=12;
    public int caretPos = 0;
    public int widthCaret=0;
    public int DichTextString=0;
    public int saveCharFrist=0;
    public int saveCharPrevious=0;
    public boolean PreviousNhoHonFirst=false;
    public static final int TEXT_GAP_X = 3;
    public int offsetX = 0;
    public static boolean isqwerty;
    public boolean co=false;
    public String stringchar=null;
    public int xline=0;
    public int Truot=0;
    //namnv
    public boolean isFlag;
	public TextField()
	{
		isFlag = false;
	}
	public TextField(boolean flag){
		isFlag = flag;
	}
	public void paint(Graphics g,String Title)
	{
			
		 //g.drawImage(isFlag?CRess.card : CRess.search, x-2, y-23, Graphics.TOP|Graphics.LEFT);       
	     // paint search and card
		  if(isFlag)
			  g.drawImage(CRess.card, (CCanvas.s_iWcanvas - CRess.search.getWidth())/2, CCanvas.s_iHcanvas - CRess.keyboard.getHeight() - CRess.menubar.getHeight() - CRess.card.getHeight(), Graphics.TOP|Graphics.LEFT);     
		  else
			  g.drawImage(CRess.search, (CCanvas.s_iWcanvas - CRess.search.getWidth())/2, CCanvas.s_iHcanvas - CRess.keyboard.getHeight() - CRess.menubar.getHeight()- CRess.search.getHeight(), Graphics.TOP|Graphics.LEFT);     
		
		// paint String Text
		  if(Title !="" && Title.length() > 0)
		  {
			  if("GT-S5360".equals(Build.MODEL))
			  {
				 BitMapFont.FONT_MEDIUM = 13;
		    	 CCanvas.mediumfont.drawString(g, Title, x + 2 , y- 15, 0, 0xff0000, 0x090b7a, false);
		    	 BitMapFont.FONT_MEDIUM = 15;	
			  }
			  else
				 CCanvas.mediumfont.drawString(g, Title, x + 2 , y- 20, 0, 0xff0000, 0x090b7a, false);
			  
			  
		  }
		  
		 g.setClip(x+4, y, width, height);
	     int khoangdu=0;
	     if(CCanvas.smallfont.stringWidth(TextString)>width-2)
	     {
	    	 khoangdu=CCanvas.smallfont.stringWidth(TextString)-width+4;
	     }
	     CCanvas.mediumfont.drawString(g, TextString, x+3-khoangdu, y+(height-CharHeight)/2, 0, 0x000000, 0x090b7a, false);
	     
	     // paint Caret
	     if(delaytimeCaret>10 && delaytimeCaret<20)
	     {	
			  g.setColor(0x000000);	    	 
			  g.drawLine(x+5+CCanvas.smallfont.stringWidth(TextString.substring(0, caretPos))-widthCaret-khoangdu, y+2, x+4+CCanvas.smallfont.stringWidth(TextString.substring(0, caretPos))-widthCaret-khoangdu, y-2+height);
	     }	     
	     g.setClip(0, 0, CCanvas.s_iWcanvas, CCanvas.s_iHcanvas);
	}
	public void update() 
	{
		delaytimeCaret++;
		if(delaytimeCaret>20)
			delaytimeCaret=0;
		
		if (ActiveKey > 0)
        {
			ActiveKey--;
			if(ActiveKey==0)
			{
				indexOfActiveChar=0;
				lastKey=-1983;
			}
        }
	}
	public void KeyPressAny(int keycode)
	{
			 if (keycode == lastKey) // nhung lan tiep theo sao
	         {
				 if(TextString.length()<maxTextLength)
				 {
					 indexOfActiveChar = (indexOfActiveChar + 1) % print[keycode-'0'].length();
		             char c = print[keycode-'0'].charAt(indexOfActiveChar);
		             String ttext = TextString.substring(0, caretPos - 1) + c;
		             if (caretPos < TextString.length())
		                 ttext = ttext + TextString.substring(caretPos, TextString.length());
		             TextString = ttext;
		             ActiveKey=12;
		             widthCaret=2;
		             delaytimeCaret=10;
				 }
	         }
			 else  // nhan lan dau 
	         {	
				 	 
					 if(TextString.length()<maxTextLength)
					 {
						 indexOfActiveChar=0;
						 char c = print[keycode-'0'].charAt(indexOfActiveChar);
						 String ttext = TextString.substring(0, caretPos) + c;
			             if (caretPos < TextString.length())
			             {
			            	 ttext = ttext + TextString.substring(caretPos, TextString.length());
			             }
			             TextString = ttext;
			             caretPos++;
			             widthCaret=2;
			             ActiveKey=12;
			             delaytimeCaret=10;
					 }
	         }
	        lastKey=keycode;
	}
	public void keyPressedAscii(int keycode) {
        if (TextString.length() < maxTextLength) {
            String ttext = TextString.substring(0, caretPos) + (char) keycode;
            if (caretPos < TextString.length())
                ttext = ttext + TextString.substring(caretPos, TextString.length());
            TextString = ttext;
            caretPos++;
            widthCaret=2;
        }
    }
	
	public void setBound(int x,int y,int width,int height)
	{
		this.x=x;
		this.y=y;
		this.width=width;
		this.height=height;
	}
	 
}
